from django.shortcuts import render
from collections import OrderedDict

# Create your views here.
from django.views import View
# from goods.models import GoodsCategory, GoodsChannel, GoodsChannelGroup
from contents.models import ContentCategory, Content
from .utils import get_categories


class IndexView(View):
    def get(self, req):
        """提供首页广告页面"""

        # 查询并展示商品分类
        categories = get_categories()

        # 查询首页广告数据
        # 查询所有的[广告类别]
        contents = OrderedDict()
        content_categories = ContentCategory.objects.all()
        for content_category in content_categories:
            # 查询出上架的广告，并排序
            # contents[content_category.key] = ContentCategory.content_set.filter(status=True).order_by('sequence')
            contents[content_category.key] = content_category.content_set.filter(status=True).order_by('sequence')

        # 使用广告类别 查询出该类别对应的所有的广告内容

        # 构造上下文
        context = {
            'categories': categories,
            'contents': contents,
        }

        return render(req, 'index.html', context)


